var express = require('express')
var router = express.Router()

var fs = require('fs')
var path = require('path')
var multer = require('multer') //multer 主要用于文件上传
    //临时上传目录
var upload = multer({ dest: 'tmp_uploads/' })
    //第三方config获取项目的配置文件
var upload_config = require('config').get('upload_config')
    // console.log(upload_config);

router.post('/upload', upload.single('file'), (req, res, next) => {
    // console.log(req.file);
    var fileEXArray = req.file.originalname.split('.')
    var ext = fileEXArray[fileEXArray.length - 1]
    var targetPath = req.file.destination + req.file.filename + '.' + ext
        // console.log(targetPath);
    fs.rename(req.file.path, targetPath, (err) => {
        if (err) return res.ResData("文件上传失败!")
    })
    res.ResData("文件上传成功", 200, {
        "tmp_path": targetPath,
        "url": upload_config.baseURL + targetPath
    })
})
module.exports = router